package com.example.oasystem.controller.process;

import com.example.oasystem.dao.process.AoaProcessDaoOfSun;
import com.example.oasystem.entity.dept.AoaDept;
import com.example.oasystem.entity.page.Page;
import com.example.oasystem.entity.position.AoaPosition;
import com.example.oasystem.entity.process.AoaProcessOfSun;
import com.example.oasystem.entity.process.AoaReviewedOfSun;
import com.example.oasystem.entity.process.AoaUserProcessOfSun;
import com.example.oasystem.entity.status.AoaStatusList;
import com.example.oasystem.entity.user.AoaUser;
import com.example.oasystem.service.AoaPositionService;
import com.example.oasystem.service.AoaProcessServiceOfSun;
import com.example.oasystem.service.AoaUserProcessServiceOfSun;
import com.example.oasystem.service.AoaUserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.SimpleFormatter;

@Controller
public class AoaProcessControllerOfSun {


    @Autowired
    private AoaUserProcessServiceOfSun aoaUserProcessServiceOfSun;
    @Autowired
    private AoaProcessServiceOfSun aoaProcessServiceOfSun;



    @RequestMapping("/processpaging")
    public String processPaging(HttpServletRequest request, Model model,
                                @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                @RequestParam(value = "baseKey", defaultValue = "") String baseKey
    ) {
        if (baseKey.equals(","))
        {
            baseKey="";
        }
        AoaUser user = (AoaUser)request.getSession().getAttribute("user");
        String userName = user.getUserName();
        PageInfo<AoaProcessOfSun> prolist = aoaProcessServiceOfSun.selectProcessAndTypeAndStatus(pageNum,pageSize,baseKey,userName);
        Page page=new Page(prolist.getSize(), Math.toIntExact(prolist.getTotal()),prolist.getPages(),prolist.getPageNum(),prolist.isIsFirstPage(),prolist.isIsLastPage());
        model.addAttribute("prolist",prolist.getList());
        model.addAttribute("page",page);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("url","processpaging");
        return "process/audtable";
    }

    @RequestMapping("/auditing")
    public String selectViewInformation(@RequestParam("id") long id,
                                        HttpServletRequest request, Model model,
                                        @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum,
                                        @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                                        @RequestParam(value = "baseKey", defaultValue = "") String baseKey
    ){
        List<AoaReviewedOfSun> ofSuns = aoaProcessServiceOfSun.reviewedSize(id,25);
        AoaProcessOfSun aoaProcessOfSun = aoaProcessServiceOfSun.selectUserProcess(id);
        aoaProcessOfSun.setSize(ofSuns.size());
        System.out.println("aoaPositions1111111111111111111111111111111111111111111111 = " + aoaProcessOfSun);
        List<AoaUserProcessOfSun> aoaUsers = aoaUserProcessServiceOfSun.selectUserProcess(id);
        AoaUser user = (AoaUser)request.getSession().getAttribute("user");

        if (baseKey.equals(","))
        {
            baseKey="";
        }
        PageInfo<AoaUserProcessOfSun> prolist = aoaUserProcessServiceOfSun.selectUsers(pageNum,pageSize,baseKey,user.getFatherId());
        List<AoaPosition> positions = aoaUserProcessServiceOfSun.selectPosition();
        List<AoaDept> depts = aoaUserProcessServiceOfSun.selectDept();
        Page page=new Page(prolist.getSize(), Math.toIntExact(prolist.getTotal()),prolist.getPages(),prolist.getPageNum(),prolist.isIsFirstPage(),prolist.isIsLastPage());
        model.addAttribute("emplist",prolist.getList());
        model.addAttribute("page",page);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("url","addressBookpaging");
        model.addAttribute("revie",aoaUsers);
        model.addAttribute("process",aoaProcessOfSun);
        model.addAttribute("poslist",positions);
        model.addAttribute("deptlist",depts);
        model.addAttribute("user",user);
        return "process/audetail";
    }





    @RequestMapping("/addressBookpaging")
    public String addressBookpaging(HttpServletRequest request, Model model,
                                    @RequestParam(value = "pageNum", required = false,defaultValue = "1") int pageNum,
                                    @RequestParam(value = "pageSize", defaultValue = "4") int pageSize,
                                    @RequestParam(value = "baseKey", defaultValue = "") String baseKey
    ) {
        AoaUser user = (AoaUser)request.getSession().getAttribute("user");
        if (baseKey.equals(","))
        {
            baseKey="";
        }
        PageInfo<AoaUserProcessOfSun> prolist = aoaUserProcessServiceOfSun.selectUsers(pageNum,pageSize,baseKey,user.getFatherId());
        List<AoaPosition> positions = aoaUserProcessServiceOfSun.selectPosition();
        List<AoaDept> depts = aoaUserProcessServiceOfSun.selectDept();
        Page page=new Page(prolist.getSize(), Math.toIntExact(prolist.getTotal()),prolist.getPages(),prolist.getPageNum(),prolist.isIsFirstPage(),prolist.isIsLastPage());
        model.addAttribute("emplist",prolist.getList());
        model.addAttribute("page",page);
        model.addAttribute("baseKey",baseKey);
        model.addAttribute("url","addressBookpaging");
        model.addAttribute("poslist",positions);
        model.addAttribute("deptlist",depts);
        return "common/reciversOfSun";
    }


    @RequestMapping("/susave")
    public String addProcess(HttpServletRequest request,
                             @RequestParam("proId")long proId,
                             @RequestParam("statusId")long statusId,
                             @RequestParam("userName")String userName,
                             @RequestParam(value = "advice",required = false,defaultValue = "")String advice
    ){

        System.out.println("proId11111111111111111111111111111 = " + proId);
        System.out.println("statusId 1111111111111111111111111111= " + statusId);
        System.out.println("userName 11111111111111111111111111111111= " + userName);
        System.out.println("advice1111111111111111111111= " + advice);
        AoaUser user = (AoaUser)request.getSession().getAttribute("user");
        AoaProcessOfSun aoaProcessOfSun = aoaProcessServiceOfSun.selectProcess(proId);
        String shenuser = aoaProcessOfSun.getShenuser();
        if (shenuser==null){
            shenuser="";
        }

        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);
        System.out.println("userName .......................................= " + userName);
        Long userId = user.getUserId();
        aoaProcessServiceOfSun.updateUserReviewed(advice,time,statusId,proId,userId);
        List<AoaReviewedOfSun> reviewedOfSuns = aoaProcessServiceOfSun.reviewedSize(proId, 25);
        List<AoaReviewedOfSun> processReviewedAll = aoaProcessServiceOfSun.processReviewedAll(proId);
        if (user.getPositionId()==5 || user.getPositionId()==7){
            aoaProcessServiceOfSun.insertProcessUser(proId,statusId,statusId,userId);
            if(aoaProcessOfSun.getTypeName().equals("离职申请") && reviewedOfSuns.size()==processReviewedAll.size()){
                aoaProcessServiceOfSun.updateResign(proId,advice,0);
            }else if(aoaProcessOfSun.getTypeName().equals("费用报销")){
                aoaProcessServiceOfSun.updateBursement(proId,advice,0);
            }else if(aoaProcessOfSun.getTypeName().equals("加班申请")){
                aoaProcessServiceOfSun.updateOvertime(proId,advice,0);
            }else if(aoaProcessOfSun.getTypeName().equals("转正申请")){
                aoaProcessServiceOfSun.updateRegular(proId,advice,0);
            }else if(aoaProcessOfSun.getTypeName().equals("出差申请")){
                aoaProcessServiceOfSun.updateEvenction(proId,advice,0);
            }else if(aoaProcessOfSun.getTypeName().equals("请假申请")){
                aoaProcessServiceOfSun.updateHoliday(proId,advice,0);
            }
        }else {
            if (userName!=null && userName!=""){
                String[] split = userName.split(";");
                for (String s : split) {
                    long userid = aoaUserProcessServiceOfSun.selectOneUser(s);
                    aoaProcessServiceOfSun.addUserReviewed(23,proId,userid);
                }
                aoaProcessServiceOfSun.updateUserReviewed(advice,time,statusId,proId,userId);
                userName=shenuser+";"+userName;
                aoaProcessServiceOfSun.updateUserName(proId,userName);
            }
            if(aoaProcessOfSun.getTypeName().equals("离职申请")){
                if (user.getPositionId()!=3){
                    aoaProcessServiceOfSun.updateResign(proId,advice,1);
                }
                if (user.getPositionId()==3){
                    aoaProcessServiceOfSun.updateResign(proId,advice,2);

                }
            }else if(aoaProcessOfSun.getTypeName().equals("费用报销")){
                aoaProcessServiceOfSun.updateBursement(proId,advice,1);
            }else if(aoaProcessOfSun.getTypeName().equals("加班申请")){
                aoaProcessServiceOfSun.updateOvertime(proId,advice,1);
            }else if(aoaProcessOfSun.getTypeName().equals("转正申请")){
                aoaProcessServiceOfSun.updateRegular(proId,advice,1);
            }else if(aoaProcessOfSun.getTypeName().equals("出差申请")){
                aoaProcessServiceOfSun.updateEvenction(proId,advice,1);
            }else if(aoaProcessOfSun.getTypeName().equals("请假申请")){
                aoaProcessServiceOfSun.updateHoliday(proId,advice,1);
            }
            long status=24;
            aoaProcessServiceOfSun.insertProcessUser(proId,status,statusId,userId);

        }


        return "redirect:/audit";
    }


}
